package com.example.dao;

import com.example.model.Lab;
import com.example.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LabDAO {
    // 查询所有实验室
    public List<Lab> findAllLabs() {
        List<Lab> labs = new ArrayList<>();
        String sql = "SELECT * FROM lab";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Lab lab = new Lab();
                lab.setLabId(rs.getLong("lab_id"));
                lab.setLabName(rs.getString("lab_name"));
                lab.setLocation(rs.getString("location"));
                lab.setCapacity(rs.getInt("capacity"));
                lab.setEquipment(rs.getString("equipment"));
                lab.setAvailableTime(rs.getString("available_time"));
                lab.setDescription(rs.getString("description"));
                labs.add(lab);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return labs;
    }

    // 根据ID查询实验室
    public Lab findLabById(long id) {
        Lab lab = null;
        String sql = "SELECT * FROM lab WHERE lab_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                lab = new Lab();
                lab.setLabId(rs.getLong("lab_id"));
                lab.setLabName(rs.getString("lab_name"));
                lab.setLocation(rs.getString("location"));
                lab.setCapacity(rs.getInt("capacity"));
                lab.setEquipment(rs.getString("equipment"));
                lab.setAvailableTime(rs.getString("available_time"));
                lab.setDescription(rs.getString("description"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lab;
    }

    // 添加实验室
    public boolean addLab(Lab lab) {
        String sql = "INSERT INTO lab (lab_name, location, capacity, equipment, available_time, description) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, lab.getLabName());
            pstmt.setString(2, lab.getLocation());
            pstmt.setInt(3, lab.getCapacity());
            pstmt.setString(4, lab.getEquipment());
            pstmt.setString(5, lab.getAvailableTime());
            pstmt.setString(6, lab.getDescription());
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新实验室
    public boolean updateLab(Lab lab) {
        String sql = "UPDATE lab SET lab_name = ?, location = ?, capacity = ?, equipment = ?, available_time = ?, description = ? WHERE lab_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, lab.getLabName());
            pstmt.setString(2, lab.getLocation());
            pstmt.setInt(3, lab.getCapacity());
            pstmt.setString(4, lab.getEquipment());
            pstmt.setString(5, lab.getAvailableTime());
            pstmt.setString(6, lab.getDescription());
            pstmt.setLong(7, lab.getLabId());
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除实验室
    public boolean deleteLab(long id) {
        String sql = "DELETE FROM lab WHERE lab_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, id);
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}